﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class MCSQListControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        DropDownList DropDownList1;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            DropDownList1 = (DropDownList)FindControl("DropDownList1");
            if (!base.Page.IsPostBack)
            {
                BindDll();
                DataBind();
            }
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
            CustomGridView1.RowDeleting+=new GridViewDeleteEventHandler(CustomGridView1_RowDeleting);
            Button btnSeach = (Button)FindControl("btnSeach");
            btnSeach.Click += new EventHandler(btnSeach_Click);
            
        }

       
       protected void btnSeach_Click(object sender, EventArgs e)
        {
            DataBind();
        }

       protected void  CustomGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            string personid = CustomGridView1.DataKeys[e.RowIndex].Value.ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("zs_mcyhcqb", "id", personid);
            if (del)
            {
                DataBind();
                HttpContext.Current.Response.Write("<script>alert('删除成功！');window.location.href=location.href;</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功！')", true);
            }
        }


       protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
       {
           DataBind();
       }


        public override void DataBind()
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            string sql = string.Empty;
            TextBox txtName = (TextBox)FindControl("txtName");
            sql = "Select t.*,(select 项目名称 from zs_project where id=t.pid) 项目名称 from zs_mcyhcqb t where 1=1 ";
            if (!string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                sql += " and  申请人姓名 like '%" + txtName.Text.Trim() + "%'";
            }
            string strRoleId = base.Page.Request["roleid"];
            string strType = base.Page.Request["type"];
            if (!string.IsNullOrEmpty(strRoleId)) 
            {
                sql += " and roleid='" + strRoleId + "'";
            }
            if (!string.IsNullOrEmpty(strType))
            {
                if (strType!="全部")
                {
                    sql += " and 用户类型 ='" + strType + "'";
                }
                
            }
            if (!string.IsNullOrEmpty(DropDownList1.SelectedValue)) {
                sql += " and roleid='" + DropDownList1.SelectedValue + "'";
            }
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.PageSize = 10;
            CustomGridView1.DataKeyNames = new string[] { "ID" };
            CustomGridView1.DataBind();
           
        }
        protected void BindDll() {
            DropDownList1.Items.Add(new ListItem("全部", ""));
            DataTable dt = _EluZSDataProvider.GetTable("Select t.id,substr(t.rolename,0,3) rolename From zs_role t Where t.rolename Like '%征收部门%'");
            if (dt.Rows.Count > 0) {
                for (int i = 0; i < dt.Rows.Count; i++) {
                    DropDownList1.Items.Add(new ListItem(dt.Rows[i]["rolename"].ToString(), dt.Rows[i]["id"].ToString()));
                }
            }
        }


             
    }
}
